package enterprise.manage.web.controller.stock;

import enterprise.manage.dao.*;
import enterprise.manage.entity.*;
import enterprise.manage.service.StockService;
import enterprise.manage.service.SysNoService;
import enterprise.manage.util.StringUtilsExt;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.Date;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User:zhenmingyue
 * Date: 2012-11-25
 * Desc:
 */
public class StockSaveController extends AbstractController {
    @Autowired
    StockDao stockDao;
    @Autowired
    StockService stockService;
    @Autowired
    OrderDao orderDao;
    @Autowired
    ProductDao productDao;
    @Autowired
    ProductTypeDao productTypeDao;

    @Autowired
    PurchaseDao purchasesDao;

    @Autowired
    SysNoService sysNoService;
    public static final String DEFAULT_PAGE = "stock/view";

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String id = StringUtilsExt.trimToEmpty(request.getParameter("id"));
        String stockNo = StringUtilsExt.trimToEmpty(request.getParameter("stockNo"));
        String products = StringUtilsExt.trimToEmpty(request.getParameter("products"));

        String inOrOut = StringUtilsExt.trimToEmpty(request.getParameter("inOrOut"));

        String orderId = StringUtilsExt.trimToEmpty(request.getParameter("orderId"));
        String purchasesId = StringUtilsExt.trimToEmpty(request.getParameter("purchasesId"));
        if (StringUtils.isBlank(stockNo)) {
            stockNo = sysNoService.getNo("S");
        }
        Stocks entity = getEntity(id);
        Orders order = null;
        Products product = null;
        Purchases purchases = null;
        Set inProducts = entity.getInProducts();
        Set outProducts = entity.getOutProducts();

        if (StringUtils.isNotBlank(orderId)) {
            order = orderDao.findByUnique("id", orderId);
            entity.setOrder(order);
            entity.setOrderNo(order.getOrderNo());
        }
        if (StringUtils.isNotBlank(products)) {
            String[] p = products.split(";");
            for (String s : p) {
                String[] array = s.split(",");
                String type = array[0];
                String brand = array[1];
                String model = array[2];
                String serialNo = array[3];
                String price = array[4]; 
                if (inOrOut.contains("入库")) {
                    Products productObj = new Products();
                    ProductType productType = productTypeDao.getProductBy(type, brand, model);
                    productObj.setProductType(productType);
                    productObj.setCreateTime(Calendar.getInstance());
                    productObj.setInPrice(price);
                    productObj.setSerialNo(serialNo);
                    inProducts.add(productObj);
                } else {
                    Products productObj = productDao.getProduct(serialNo);
                    productObj.setOutPrice(price);
                    outProducts.add(productObj);
                }
            }
        }
        if (StringUtils.isNotBlank(purchasesId)) {
            purchases = purchasesDao.get(Long.parseLong(purchasesId));
            entity.setPurchase(purchases);
        }

        entity.setInOrOut(inOrOut);
        entity.setStockNo(stockNo);
        stockService.save(entity);
        entity = stockDao.findByUnique("id", entity.getId());
        request.setAttribute("entity", entity);
        //成功跳转到 VIEW 页面
        request.setAttribute("method", "view");
        return new ModelAndView(DEFAULT_PAGE);
    }

    public Stocks getEntity(String id) {
        if (StringUtils.isNotBlank(id)) {
            return stockDao.findByUnique("id", Long.parseLong(id));
        } else {
            return new Stocks();
        }
    } 


}